package com.view.houseon;

import java.util.ArrayList;

import com.controller.houseon.ControlHouseON;

import com.model.houseon.EscenarioBean;
import com.presentacion.houseon.R;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;


public class ListaEscenariosActivity extends Activity {

	ListView listEs;
	ArrayList<EscenarioBean> lista;
	EscenarioBean e1;
	int idCasa, idTE;
	static final int id_demo=4;
	
	
	ImageButton ibtn_inicio;
	ImageButton ibtn_logout;
	ImageButton ibtn_filtro;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_lista_escenarios);
		
			listEs = (ListView)findViewById(R.id.lstEscenario);
			Bundle b= getIntent().getExtras();
			idTE=b.getInt("idTipoE");
			idCasa=b.getInt("idCasa");
			
			ibtn_logout=(ImageButton)findViewById(R.id.ibtnInformacion);
			ibtn_logout.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					//Toast.makeText(ListaEscenariosActivity.this, String.valueOf("logout.."), Toast.LENGTH_SHORT).show();
				    AlertDialog.Builder alert = new AlertDialog.Builder(ListaEscenariosActivity.this);
	                alert.setTitle("Desea Salir?");
	                alert.setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
	                    public void onClick(DialogInterface dialog, int whichButton) {  
	                        salir();
	                        }});
	                           alert.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {  
	                               public void onClick(DialogInterface dialog, int whichButton) {    
	                            }});
	                       alert.show();
				}
			});
			
			ibtn_inicio=(ImageButton)findViewById(R.id.ibtnHome);
			ibtn_inicio.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					//Toast.makeText(ListaEscenariosActivity.this, String.valueOf("inicio.."), Toast.LENGTH_SHORT).show();
					 Intent i = new Intent(ListaEscenariosActivity.this, ListaCasaActivity.class);
					 startActivity(i);
				}
			});
			

			ibtn_filtro=(ImageButton)findViewById(R.id.ibtnEscenarios);
			ibtn_filtro.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					//Toast.makeText(ListaEscenariosActivity.this, String.valueOf("filtro.."), Toast.LENGTH_SHORT).show();
					 Intent i = new Intent(ListaEscenariosActivity.this, CategoriasCasaActivity.class);
					 startActivity(i);
				}
			} );
			
			
			lista=ControlHouseON.getInstancia().ListaEscenarioxTipoE(idTE, idCasa);
	        
	        ItemEscenarioAdapter1 item = new ItemEscenarioAdapter1(ListaEscenariosActivity.this, lista);
	        listEs.setAdapter(item);
	        registerForContextMenu(listEs);
	        
	        
	    
			
	        
	        listEs.setOnItemClickListener(new OnItemClickListener() {

				public void onItemClick(AdapterView<?> l, View v, int position,
						long id) {
					//Toast.makeText(ListaEscenariosActivity.this, String.valueOf("id: "+ id), Toast.LENGTH_SHORT).show();
					// TODO Auto-generated method stub
					if(ControlHouseON.getInstancia().CurrentUsuario.getTipoUsuario().getIdTipoUsuario()!=id_demo){
					Intent i = new Intent(ListaEscenariosActivity.this,ListaDispositivosActivity.class);
					i.putExtra("idEscenario",(int)id);
					startActivity(i);
					}
					else{
						
						Intent i = new Intent(ListaEscenariosActivity.this,ListaDispositivosDemoActivity.class);
						i.putExtra("idEscenario",(int)id);
						startActivity(i);
					}
					
				}
			});
	    }
	
	 public  void salir(){
	       Intent i = new Intent(); 
	                  i.setAction( Intent.ACTION_MAIN); 
	                  i.addCategory( Intent.CATEGORY_HOME); 
	                  i.setFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP); 
	                  startActivity(i); 
	                  android.os.Process.killProcess( android.os.Process.myPid()); 
	                  finish();
	  }
	
	public static final int id_editar = 1;
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.setHeaderTitle("Menu Opciones");  
		menu.add(0, id_editar, 0, "Editar");
		
	}
	
	 @Override
	    public boolean onContextItemSelected(MenuItem item) {
	        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
	        
	        e1=lista.get(info.position);
	       // Toast.makeText(this, "id" + e1.getIdEscenario(), Toast.LENGTH_SHORT).show();
	        
	        
	        AlertDialog.Builder builder = new AlertDialog.Builder(ListaEscenariosActivity.this);
		    builder.setTitle("Cuadro de Edicion");
		    builder.setView(getLayoutInflater().inflate(R.layout.dialog_personal, null))
		    // Add action buttons
		           .setPositiveButton("Guardar", new DialogInterface.OnClickListener() {
		               @Override
		               public void onClick(DialogInterface dialog, int id) {
		            	   
		            	   //e1.getIdEscenario();
		            	   Dialog diag= (Dialog)dialog;
		            	   EditText nombre_escenario= (EditText)diag.findViewById(R.id.txt_edit_nombre);
		            	   String nombre=nombre_escenario.getText().toString();
		            	   
		            	   if(ControlHouseON.getInstancia().ActualizarEscenarioWS(e1.getIdEscenario(), nombre, "")){			            	   
		            		   Toast.makeText(ListaEscenariosActivity.this, String.valueOf("Se actualizo"), Toast.LENGTH_SHORT).show();
		            		   Intent i= new Intent(ListaEscenariosActivity.this,ListaEscenariosActivity.class);
		            		   i.putExtra("idTipoE", idTE);
		            		   i.putExtra("idCasa", idCasa);
		            		   ListaEscenariosActivity.this.finish();
		            		   startActivity(i);
		            	   }
		            	   else
		            		   Toast.makeText(ListaEscenariosActivity.this, String.valueOf("Error .."), Toast.LENGTH_SHORT).show();
		               }
		           })
		           .setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
		               public void onClick(DialogInterface dialog, int id) {
		                   dialog.cancel();
		               }
		           });      
		    
		    builder.show();
	        return true;
	    }
	
	    @Override
	    public boolean onCreateOptionsMenu(Menu menu) {
	        getMenuInflater().inflate(R.menu.lista_escenarios, menu);
	        return true;
	    }

		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
			// TODO Auto-generated method stub
			//Toast.makeText(getApplicationContext(), ((TextView)arg1).getText().toString(), Toast.LENGTH_LONG).show();
		}
		
		
		public class ItemEscenarioAdapter1 extends BaseAdapter{
			  protected Activity activity;
			  protected ArrayList<EscenarioBean> items;
			         
			  public ItemEscenarioAdapter1(Activity activity, ArrayList<EscenarioBean> items) {
			    this.activity = activity;
			    this.items = items;
			  }
			 
			  public int getCount() {
			    return items.size();
			  }
			 
			  public Object getItem(int position) {
			    return items.get(position);
			    
			  }
			 
			  public long getItemId(int position) {
			    return items.get(position).getIdEscenario();
			  }
			 
			  public View getView(int position, View convertView, ViewGroup parent) {
			    View vi=convertView;
			         
			    if(convertView == null) {
			      LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			      vi = inflater.inflate(R.layout.item_escenario, null);
			      
			    }
			             
			    EscenarioBean item = items.get(position);
			         
			    ImageView image = (ImageView) vi.findViewById(R.id.imgEscenario);
			    int imageResource = activity.getResources().getIdentifier("drawable/"+item.getImagen(), null, activity.getPackageName());
			    image.setImageDrawable(activity.getResources().getDrawable(imageResource));
			         
			    TextView nombre = (TextView) vi.findViewById(R.id.txtItemEscenario);
			    nombre.setText(item.getNombre());
			         
			   TextView tipo = (TextView) vi.findViewById(R.id.txt_tipo_escenario);
			   tipo.setText(item.getTipoEscenario().getNombre());
			 
			    return vi;
			  }
		}


}

